/*
* @Author: ylq
 * @Date: 2021-08-23 15:42:14
 * @Desc: Plop 创建文件
 * @Last Modified by: ylq
 * @Last Modified time: 2022-04-22 09:20:05
 */
const _ = require('lodash')

function onlyName(txt) {
  let arr = txt.split('/')
  return arr.length > 1 ? arr[arr.length - 1] : txt
}

module.exports = (plop) => {
  // 设置工具函数 强制大写开头
  plop.setHelper('kebabCase', txt => _.kebabCase(txt)) //加-
  plop.setHelper('bigCamelCase', txt => _.upperFirst(_.camelCase(txt)))
  plop.setHelper('onlyName', onlyName)

  const prompts = [
    {
      type: 'input',
      name: 'inputName',
      message: '请输入组件名称/页面名称/API名称',
      validate(val) {
        if (RegExp(/^[A-Za-z]+[/a-z0-9A-Z_-]*$/).test(val.trim())) {
          return true
        } else {
          return '格式错误'
        }
      },
    },
  ]

  // 新增组件
  plop.setGenerator('component', {
      description: '这是一个创建组件的生成器',
      prompts,
      actions(data) {
        let arr = data.inputName.split('/').map(el => _.kebabCase(el))
        console.log(42,arr)
        let name = arr[arr.length - 1]
        // arr[arr.length - 1] = _.upperFirst(_.camelCase(name))
        return [
          {
            type: 'add',
            path: `src/components/${arr.join('/')}/{{kebabCase name}}.vue`, // 目标路径
            templateFile: 'plop-templates/component/template.hbs', // 源模板路径
            data: {
              name,
            },
          },
          {
            type: 'add',
            path: `src/components/${arr.join('/')}/{{kebabCase name}}.less`, // 目标路径
            templateFile: 'plop-templates/component/less.hbs', // 源模板路径
            data: {
              name,
            },
          },
        ]
      },
  });
  plop.setGenerator('page', {
    description: 'add views | 新增页面',
    prompts,
    actions(data) {
      // let arr = data.inputName.split('/')
      // let name = arr[arr.length - 1]
      // arr[arr.length - 1] = _.upperFirst(_.camelCase(name))
      let arr = data.inputName.split('/').map(el => _.kebabCase(el))
      // console.log(42,arr)
      // let name = arr[arr.length - 1]
      return [
        {
          type: 'add', //add:新增
          path: `src/views/${arr.join('/')}/{{kebabCase name}}.vue`, // 目标路径
          templateFile: 'plop-templates/views/template.hbs', // 源模板路径
          data: {
            name: onlyName(data.inputName),
          },
        },
        {
          type: 'add', //add:新增
          path: `src/views/${arr.join('/')}/{{kebabCase name}}.less`, // 目标路径
          templateFile: 'plop-templates/views/less.hbs', // 源模板路径
          data: {
            name: onlyName(data.inputName),
          },
        },
      ]
    },
  })

  plop.setGenerator('service', {
    description: 'add API service | 新增API服务',
    prompts,
    actions(data) {
      // let arr = data.inputName.split('/')
      let arr = data.inputName.split('/').map(el => _.kebabCase(el))
      // let name = arr[arr.length - 1]
      // arr[arr.length - 1] = _.upperFirst(_.camelCase(name))
      return [
        {
          type: 'add', //add:新增
          path: `src/api/{{kebabCase name}}-service.js`, // 目标路径
          templateFile: 'plop-templates/api/template.hbs', // 源模板路径
          data: {
            name: onlyName(data.inputName),
          },
        },
      ]
    },
  })


};
